package com.skillscompetition.skillscompetition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.skillscompetition.skillscompetition.bean.SkillScontest;
import com.skillscompetition.skillscompetition.mapper.SkillScontestMapper;
import com.skillscompetition.skillscompetition.service.ISkillScontestService;
import com.skillscompetition.skillscompetition.vo.SkillScontestVo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 * 技能表 服务实现类
 * </p>
 *
 * @author lwg
 * @since 2019-11-09
 */
@Service
public class SkillScontestServiceImpl extends ServiceImpl<SkillScontestMapper, SkillScontest> implements ISkillScontestService {
    @Autowired
    private SkillScontestMapper skillScontestMapper;

    @Override
    public PageInfo<SkillScontestVo> getExam(Integer page, Integer pageSize, String c) {
        PageHelper.startPage(page, pageSize);
        QueryWrapper<SkillScontest> wrapper = new QueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(c), "number", c);
        List<SkillScontestVo> exam = skillScontestMapper.getExam(wrapper);
        PageInfo<SkillScontestVo> info = new PageInfo<>(exam);
        return info;
    }

    @Override
    @Transactional
    public void updateTheorys(List<SkillScontestVo> theorys) {
        SkillScontest skillScontest = null;
        List<SkillScontest> scontests = new ArrayList<>();
        for (SkillScontestVo skillScontestVo : theorys) {
            skillScontest = new SkillScontest();
            skillScontest.setId(skillScontestVo.getId());
            skillScontest.setTheorys(skillScontestVo.getTheorys());
            scontests.add(skillScontest);
        }
        this.updateBatchById(scontests);
    }

    @Override
    public PageInfo<SkillScontest> getSkillSOperation1(Integer page, Integer pageSize, String c) {
        PageHelper.startPage(page, pageSize);
        QueryWrapper<SkillScontest> wrapper = new QueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(c), "number", c);
        List<SkillScontest> skillSOperations = skillScontestMapper.getSkillSOperation(wrapper);
        PageInfo<SkillScontest> info = new PageInfo<>(skillSOperations);
        return info;
    }

    @Override
    @Transactional
    public boolean saveSkillSOperation(List<SkillScontest> operration) {
        boolean flag = this.updateBatchById(operration);
        return flag;
    }

    /**
     * 总体成绩刷新
     */
    @Override
    public void refresh() {
        QueryWrapper<SkillScontest> wrapper = new QueryWrapper<>();
        List<SkillScontest> skillScontests = skillScontestMapper.getSkillSOperation(wrapper);
        for (SkillScontest skillScontest : skillScontests) {
            float total = (skillScontest.getSubtotal1() * 0.4f) + (skillScontest.getSubtotal2() * 0.3f) + (skillScontest.getSubtotal3() * 0.3f);
            skillScontest.setPracticalOperationSum(total);
            float personageScores = (skillScontest.getTheorys() * 0.4f) + (total * 0.6f);
            skillScontest.setPersonageScores(personageScores);
        }
        this.updateBatchById(skillScontests);
    }

    /**
     * 总体成绩查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<SkillScontest> massingList(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        QueryWrapper<SkillScontest> wrapper = new QueryWrapper<>();
        List<SkillScontest> skillSOperations = skillScontestMapper.getSkillSOperation(wrapper);
        PageInfo<SkillScontest> info = new PageInfo<>(skillSOperations);
        return info;
    }

    @Override
    public void export(HttpServletResponse response) {

        //查询需要的数据
        try{
            //加载模板
            Resource resource = new ClassPathResource("excel/grande.xlsx");

            QueryWrapper<SkillScontest> wrapper = new QueryWrapper<>();
            List<SkillScontest> skillScontests = baseMapper.getSkillSOperation(wrapper);

            //3.根据文件流，加载指定的工作簿
            FileInputStream fis = new FileInputStream(resource.getFile());
            XSSFWorkbook wb = new XSSFWorkbook(fis);
            //4.读取工作表
            Sheet sheet = wb.getSheetAt(0);
            //5.抽取公共的样式

            Row styleRow = sheet.getRow(4);

            CellStyle[] styles = new CellStyle[styleRow.getLastCellNum()];
            for (int i = 0; i < styleRow.getLastCellNum(); i++) {
                styles[i] = styleRow.getCell(i).getCellStyle();
            }
            //6.构造每行和单元格数据
            AtomicInteger datasAi = new AtomicInteger(4);
            Cell cell = null;
            for (SkillScontest skillScontest: skillScontests) {
                Row dataRow = sheet.createRow(datasAi.getAndIncrement());
                //编号
                cell = dataRow.createCell(0);
                cell.setCellValue(skillScontest.getNumber());
                System.out.println(skillScontest.getNumber());
                cell.setCellStyle(styles[0]);
                //姓名
                cell = dataRow.createCell(1);
                cell.setCellValue(skillScontest.getName());
                cell.setCellStyle(styles[1]);

                //理论成绩
                cell = dataRow.createCell(2);
                cell.setCellValue(skillScontest.getTheorys());
                cell.setCellStyle(styles[2]);

                //合计
                cell = dataRow.createCell(3);
                cell.setCellValue(skillScontest.getPracticalOperationSum());
                cell.setCellStyle(styles[3]);

                /*小计1*/
                cell = dataRow.createCell(4);
                cell.setCellValue(skillScontest.getSubtotal1());
                cell.setCellStyle(styles[4]);

                /*裁判1*/
                cell = dataRow.createCell(5);
                cell.setCellValue(skillScontest.getOperation1Referee1());
                cell.setCellStyle(styles[5]);

                /*裁判2*/
                cell = dataRow.createCell(6);
                cell.setCellValue(skillScontest.getOperation1Referee2());
                cell.setCellStyle(styles[6]);

                /*裁判3*/
                cell = dataRow.createCell(7);
                cell.setCellValue(skillScontest.getOperation1Referee3());
                cell.setCellStyle(styles[7]);

                /*裁判4*/
                cell = dataRow.createCell(8);
                cell.setCellValue(skillScontest.getOperation1Referee4());
                cell.setCellStyle(styles[8]);

                /*裁判5*/
                cell = dataRow.createCell(9);
                cell.setCellValue(skillScontest.getOperation1Referee5());
                cell.setCellStyle(styles[9]);



                /*小计2*/
                cell = dataRow.createCell(10);
                cell.setCellValue(skillScontest.getSubtotal2());
                cell.setCellStyle(styles[10]);

                /*裁判1*/
                cell = dataRow.createCell(11);
                cell.setCellValue(skillScontest.getOperation2Referee1());
                cell.setCellStyle(styles[11]);

                /*裁判2*/
                cell = dataRow.createCell(12);
                cell.setCellValue(skillScontest.getOperation2Referee2());
                cell.setCellStyle(styles[12]);

                /*裁判3*/
                cell = dataRow.createCell(13);
                cell.setCellValue(skillScontest.getOperation2Referee3());
                cell.setCellStyle(styles[13]);

                /*裁判4*/
                cell = dataRow.createCell(14);
                cell.setCellValue(skillScontest.getOperation2Referee4());
                cell.setCellStyle(styles[14]);

                /*裁判5*/
                cell = dataRow.createCell(15);
                cell.setCellValue(skillScontest.getOperation2Referee5());
                cell.setCellStyle(styles[15]);


                /*小计3*/
                cell = dataRow.createCell(16);
                cell.setCellValue(skillScontest.getSubtotal3());
                cell.setCellStyle(styles[16]);

                /*裁判1*/
                cell = dataRow.createCell(17);
                cell.setCellValue(skillScontest.getOperation3Referee1());
                cell.setCellStyle(styles[17]);

                /*裁判2*/
                cell = dataRow.createCell(18);
                cell.setCellValue(skillScontest.getOperation3Referee2());
                cell.setCellStyle(styles[18]);

                /*裁判3*/
                cell = dataRow.createCell(19);
                cell.setCellValue(skillScontest.getOperation3Referee3());
                cell.setCellStyle(styles[19]);

                /*裁判4*/
                cell = dataRow.createCell(20);
                cell.setCellValue(skillScontest.getOperation3Referee4());
                cell.setCellStyle(styles[20]);

                /*裁判5*/
                cell = dataRow.createCell(21);
                cell.setCellValue(skillScontest.getOperation3Referee5());
                cell.setCellStyle(styles[21]);

                cell = dataRow.createCell(22);
                cell.setCellValue(skillScontest.getPersonageScores());
                cell.setCellStyle(styles[22]);
            }
            String fileName = URLEncoder.encode("总成绩信息表.xlsx", "UTF-8");
            response.setContentType("application/octet-stream");
            response.setHeader("content-disposition", "attachment;filename=" + new
                    String(fileName.getBytes("ISO8859-1")));
            response.setHeader("filename", fileName);
            wb.write(response.getOutputStream());
        }catch (Exception e){
            e.printStackTrace();
        }

    }


}
